#ifndef LOGTOOLS_H
#define LOGTOOLS_H

#include <QMainWindow>
#include <QMap>
#include <QList>
#include <QStringList>
#include <QProcess>
#include <QDateTime>
#include <QDebug>
#include <QPalette>
#include <QPushButton>
#include <QButtonGroup>
#include <QMovie>
#include <QLabel>
#include <QLine>
#include <QFont>
#include <QAction>
#include <QTableWidget>
#include <QTableView>
#include <QStandardItemModel>
#include <QFileDialog>
#include <QDir>
#include <QFile>
#include "parserlog.h"
#include "showtable.h"

#if 0
/* 创建UI
 * 年份comboBoxYear/月份comboBoxMonth筛选框
 * 年labelYear/月labelMonth标签*/
#define YEARMONTH_COMBOBOX
#endif

#if 1
/* 创建UI
 * 全部/今天/近三天/近一周/近一个月/近三个月/近半年按钮*/
#define NEARLY_TIME
#endif

QT_BEGIN_NAMESPACE
namespace Ui { class Logtools; }
QT_END_NAMESPACE

#define NEARLYTHREEDAY   -3
#define NEARLYWEEK       -7
#define NEARLYMONTH      -30
#define NEARLYTHREEMONTH -91

typedef enum {
    enumSwitch = 0,
    enumSysInfo,
    enumSysBoot,
    enumPackage,
    enumKernel,
    enumSoftwareUpdate,
    enumPrint,
    enumBurn,
    enumAudit,
    enumSession,
    enumXorg,
    enumApplication,
    enumSwitchTimes,
    enumDebug,
    enumInfo,
    enumNotice,
    enumWarning,
    enumError,
    enumCritical,
    enumAlert,
    enumEmerg
}functionIndex;

typedef enum {
    nearlyAll = 0,
    nearlyToday,
    nearlyThreeDay,
    nearlyWeek,
    nearlyMonth,
    nearlyThreeMonth
}nearlyIndex;

typedef struct {
    functionIndex curEvent;
    QString curComboBoxApp;
    QString curComboBoxSearch;
    QList<QMap<QString, QString> > curAlreadyTofindLogList;//用于显示到table中
    QList<QMap<QString, QString> > *curLogListTempAddr;    //保存当前事件的logList的地址
    void (*funcShowTable)(QList<QMap<QString, QString> > &, QTableView *, QStandardItemModel *); //保存当前事件的显示函数
#ifdef YEARMONTH_COMBOBOX
    QString curComboBoxYear;
    QString curComboBoxMonth;
    QMap<QString, QStringList>     curComboBoxFilterMap;
    QList<QMap<QString, QString> > curLogListBack;         //用于备份当前已经搜索到的当前事件的logList
#elif defined (NEARLY_TIME)
    nearlyIndex choose;
    QList<QMap<QString, QString> > curLogListBack;         //用于备份当前已经搜索到的当前事件的logList,保存Nearly筛选完后的数据
#endif
}currentShowTableStr;

class Logtools : public QMainWindow
{
    Q_OBJECT

public:
    Logtools(QWidget *parent = nullptr);
    ~Logtools();

    //初始化窗口控件
    void logtoolsAboutLogtools();
    void logtoolsSetMainMenu();
    void logtoolsSetFilterBar();
    void logtoolsSetTableView();
    void logtoolsMenuClickedEvens();

    //公用函数
    void logtoolsGetHomePath();
    void logtoolsMessagesBox();
    void logtoolsSaveCurAlreadyTofindData(QList<QMap<QString, QString> > &);
    void logtoolsSaveCurStateHandle(functionIndex, void (*funcShowTable)(QList<QMap<QString, QString> > &, QTableView *, QStandardItemModel *));/*保存数据状态的统一入口*/
    void logtoolsProcessFilterEvent();
    QList<QMap<QString, QString> > *logtoolsEvent(functionIndex); /*返回事件所对应的loglist*/

    //Filter
    void logtoolsSetComboAppShow();
    void logtoolsSetFilterBarShow();
    void logtoolsSetFilterBarHide();

    //search Filter
    QString logtoolsGetSearchLineEdit();
    bool logtoolsSearch(QMap<QString, QString> &, QString &);
    void logtoolsFilterGeneralSearchGetData();  /*通用的搜索过滤后获得的数据*/

    //Application Filter
    void logtoolsFilterApplicationGetData();    /*应用列表combobox切换后获得的数据*/
    void logtoolsFilterSetApplicationCombobox();/*应用列表的combobox*/

#ifdef YEARMONTH_COMBOBOX
    //Year Month Filter
    void logtoolsSaveCurrentComboBoxMap(QMap<QString, QStringList>);
    void logtoolsFilterSwitchGetData();         /*开关机与登录情况combobox切换后获得的数据*/
    void logtoolsFilterSwitchTimesGetData();    /*开关机次数combobox切换后获得的数据*/
    void logtoolsFilterSetSwitchCombobox(QList<QMap<QString, QString> > &);/*开关机与登录情况的combobox*/
    void logtoolsFilterSetSwitchTimesCombobox(QList<QMap<QString, QString> > &);/*开关机次数的combobox*/
#elif defined (NEARLY_TIME)
    //Nearly Filter
    void logtoolsFilterClearNearlyButtonStatus();
    void logtoolsFilterSetNearlyButtonStyle();
    void logtoolsFilterSetNearlyBxuttonGroup();
    void logtoolsFilterSaveSelectedNearlyButton(nearlyIndex);
    void logtoolsFilterNearlyAllGetData();
    void logtoolsFilterNearlyTodayGetData();
    void logtoolsFilterNearlyThreeDayGetData();
    void logtoolsFilterNearlyWeekGetData();
    void logtoolsFilterNearlyMonthGetData();
    void logtoolsFilterNearlyThreeMonthGetData();
    void logtoolsFilterNearlyChooseHandle();
#endif

    //export zip
    void logtoolsLoadingAllLogList();
    void logtoolsChangeChmod(QString &);
    void logtoolsExportSingleLogList(QString, QString, QList<QMap<QString, QString> > &);
    void exportZip(QString &, QString &, bool);

    //gif
    void logtoolsGifDialogStart();
    void logtoolsGifDialogStop();

private:
    //切换菜单项后的处理
    void logtoolsCleanFilterCondition();
    void logtoolsSaveCurrentEvent(functionIndex);
    void logtoolsSaveCurrentComboBox();
    void logtoolsSaveCurrentLogList();
    void logtoolsSaveCurrentShowTableFunc(void (*funcShowTable)(QList<QMap<QString, QString> > &, QTableView *, QStandardItemModel *));

private slots:
    //自定义的槽函数
    void logtoolsSwitchSlots();
    void logtoolsSysInfoSlots();
    void logtoolsSysBootSlots();
    void logtoolsPackageSlots();
    void logtoolsKernelSlots();
    void logtoolsSoftwareUpdateSlots();
    void logtoolsPrintSlots();
    void logtoolsBurnSlots();
    void logtoolsAuditSlots();
    void logtoolsSessionSlots();
    void logtoolsXorgSlots();
    void logtoolsApplicationSlots();
    void logtoolsSwitchTimesSlots();
    void logtoolsDebugSlots();
    void logtoolsInfoSlots();
    void logtoolsNoticeSlots();
    void logtoolsWarningSlots();
    void logtoolsErrorSlots();
    void logtoolsCriticalSlots();
    void logtoolsAlertSlots();
    void logtoolsEmergSlots();
    void logtoolsSyslogSlots();
    void logtoolsPrintRelatedSlots();

    //ui 生成的槽函数
#ifdef YEARMONTH_COMBOBOX
    void on_ComboboxYear_Changed(int index);
    void on_ComboboxMonth_Changed(int index);
#elif defined (NEARLY_TIME)
    void on_pushButtonNearlyAll_clicked();
    void on_pushButtonNearlyToday_clicked();
    void on_pushButtonNearlyThreeDay_clicked();
    void on_pushButtonNearlyWeek_clicked();
    void on_pushButtonNearlyMonth_clicked();
    void on_pushButtonNearlyThreeMonth_clicked();
#endif
    void on_buttonSearch_clicked();
    void on_comboBoxApp_activated(const QString &arg);
    void on_pushButtonExportCur_clicked();
    void on_pushButtonExportAll_clicked();
    void on_pushButtonUpdate_clicked();
    void on_pushButtonQuit_clicked();

private:
    Ui::Logtools *ui;
    QMenuBar  *menuBar;
    QMenu     *menuView;
    QMenu     *menuAnalyze;
    QMenu     *menuLevel;
    QMenu     *menuCollect;
    QMenu     *menuHelp;
    QAction   *actSwitch;
    QAction   *actSysInfo;
    QAction   *actSysBoot;
    QAction   *actPackage;
    QAction   *actKernel;
    QAction   *actSoftwareUpdate;
    QAction   *actPrint;
    QAction   *actBurn;
    QAction   *actAudit;
    QAction   *actSession;
    QAction   *actXorg;
    QAction   *actApplication;
    QAction   *actSwitchTimes;
    QAction   *actDebug;
    QAction   *actInfo;
    QAction   *actNotice;
    QAction   *actWarning;
    QAction   *actError;
    QAction   *actCritical;
    QAction   *actAlert;
    QAction   *actEmerg;
    QAction   *actSyslog;
    QAction   *actPrintRelated;
    QAction   *actAbout;

    QList<QMap<QString, QString> > logListSwitch;
    QList<QMap<QString, QString> > logListSysInfo;
    QList<QMap<QString, QString> > logListSysBoot;
    QList<QMap<QString, QString> > logListPackage;
    QList<QMap<QString, QString> > logListKernel;
    QList<QMap<QString, QString> > logListSoftwareUpdate;
    QList<QMap<QString, QString> > logListPrint;
    QList<QMap<QString, QString> > logListBurn;
    QList<QMap<QString, QString> > logListAudit;
    QList<QMap<QString, QString> > logListSession;
    QList<QMap<QString, QString> > logListXorg;
    QList<QMap<QString, QString> > logListApplication;
    QList<QMap<QString, QString> > logListSwitchTimes;
    QList<QMap<QString, QString> > logListDebug;
    QList<QMap<QString, QString> > logListInfo;
    QList<QMap<QString, QString> > logListNotice;
    QList<QMap<QString, QString> > logListWarning;
    QList<QMap<QString, QString> > logListError;
    QList<QMap<QString, QString> > logListCritical;
    QList<QMap<QString, QString> > logListAlert;
    QList<QMap<QString, QString> > logListEmerg;

    QString exportPath;
    currentShowTableStr currentShowTable;
    QStandardItemModel *model;
    QButtonGroup *buttonGroup;
    ShowTable table;
    QDialog *loadingGif;
    QLabel  *loadingLable;
    QMovie  *loadingMovie;
};
#endif // LOGTOOLS_H
